package com.hanhai.zrb.api.biz.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.hanhai.common.util.PageHelper;
import com.hanhai.zrb.api.biz.dao.mybatis.UserBlacklistDao;
import com.hanhai.zrb.api.biz.service.UserBlacklistService;
import com.hanhai.zrb.api.biz.service.UserService;
import com.hanhai.zrb.model.user.User;
import com.hanhai.zrb.model.user.UserBlacklist;

@Service
public class UserBlacklistServiceImpl implements UserBlacklistService {

	@Autowired
	private UserBlacklistDao userBlacklistDao;

	@Resource
	private UserService userService;

	private static List<UserBlacklist> userBlacklistList;
	private Logger logger = Logger.getLogger(getClass());
	@Override
	public void add(UserBlacklist blacklist) {
		UserBlacklist dbUserBlacklist = userBlacklistDao.get(blacklist
				.getUserId());
		if (dbUserBlacklist != null) {
			return;
		}
		userBlacklistDao.add(blacklist);
	}

	@Override
	public void update(UserBlacklist blacklist) {
		userBlacklistDao.update(blacklist);
	}

	@Override
	public void delete(Long id) {
		userBlacklistDao.delete(id);
	}

	@Override
	public UserBlacklist get(Long id) {
		return userBlacklistDao.get(id);
	}

	@Override
	public UserBlacklist getByUserId(Long userId) {
		return userBlacklistDao.getByUserId(userId);
	}

	@Override
	public List<UserBlacklist> listAllNotExpired() {
		if(userBlacklistList==null){
			refreshUserBlacklist();
		}
		return userBlacklistList;
	}

	@Override
	public PageInfo listPage(Integer page, Integer rows) {
		PageHelper.startPage(page, rows);
		List<UserBlacklist> list = userBlacklistDao.list();
		List<Long> userIdList = new ArrayList<Long>();
		if (CollectionUtils.isNotEmpty(list)) {
			for (UserBlacklist blacklist : list) {
				userIdList.add(blacklist.getUserId());
			}
			// 填充User
			Map<Long, User> userMap = userService.findUserMapByUids(userIdList);
			for (UserBlacklist blacklist : list) {
				blacklist.setUser(userMap.get(blacklist.getUserId()));
			}
		}

		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;
	}

	@Override
	public List<Long> listAllUserId() {
		return userBlacklistDao.listAllUserId();
	}

	//每分钟刷新1次
	@Override
	@Scheduled(cron = "0 0/1 * * * ? ")
	public void refreshUserBlacklist() {
		userBlacklistList=userBlacklistDao.listAllNotExpired();
		logger.info("refreshUserBlacklist,size="+userBlacklistList.size());
	}

	@Override
	public List<Long> listAllUserIdListNotExpired() {
		List<UserBlacklist> list = listAllNotExpired();
		List<Long> userIdListInBlacklist = new ArrayList<Long>();
		if (CollectionUtils.isNotEmpty(list)) {
			for (UserBlacklist blacklist : list) {
				userIdListInBlacklist.add(blacklist.getUserId());
			}
		}
		return userIdListInBlacklist;
	}

}
